//
//  DUDeleteCellView.m
//  toutiao
//
//  Created by duyujin1 on 2021/7/23.
//

#import "DUDeleteCellView.h"

@interface DUDeleteCellView()

@property(nonatomic,strong,readwrite) UIView * backgroundView;
@property(nonatomic,strong,readwrite) UIButton * deleteButton;
@property(nonatomic,copy,readwrite) dispatch_block_t deleteBlock;

@end

@implementation DUDeleteCellView

-(instancetype) initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        [self addSubview: ({
            _backgroundView=[[UIView alloc] initWithFrame:self.bounds];
            _backgroundView.backgroundColor=[UIColor blackColor];
            _backgroundView.alpha=0.5;
            [_backgroundView addGestureRecognizer:({
                            UITapGestureRecognizer * tapGusture=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissDeleteView)];
                tapGusture;
            })];
            _backgroundView;
        })];
        [self addSubview:self.deleteButton];
    }
    return self;
}

-(void)showDeleteViewFromPoint:(CGPoint)point clickBlock:(dispatch_block_t) clickBlock{
    _deleteButton.frame=CGRectMake(point.x, point.y, 0, 0);
    _deleteBlock=[clickBlock copy];
    CGFloat width=100;
    CGFloat height=50;
    [[[[UIApplication sharedApplication] delegate] window] addSubview:self];
    

    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:0.5 options:UIViewAnimationOptionCurveEaseIn animations:^{
        self.deleteButton.frame=CGRectMake((self.bounds.size.width-width)/2,(self.bounds.size.height-height)/2,width,height);
    } completion:^(BOOL finished) {
        NSLog(@"");
    }];

}

-(void)dismissDeleteView{
    [self removeFromSuperview];
}

-(void)clickButton{
    if(_deleteBlock){
        _deleteBlock();
    }
    [self removeFromSuperview];
}

-(UIButton *)deleteButton{
    if(!_deleteButton){
        _deleteButton=[[UIButton alloc] initWithFrame:CGRectMake(0, 0,0,0)];
        _deleteButton.backgroundColor=[UIColor lightGrayColor];
        _deleteButton.layer.cornerRadius=25;
        [_deleteButton setTitle:@"确认删除" forState:UIControlStateNormal];
        _deleteButton.titleLabel.textAlignment=NSTextAlignmentCenter;
        [_deleteButton addTarget:self action:@selector(clickButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _deleteButton;
}

@end
